import $ from 'jquery';
import 'common/css/com.less';
import './index.less';
import 'layout/header/index.js';
import SideBar from 'layout/sidebar/index.js';

const apiPage = {
  iframeWrapper: $('#iframe_wrapper'),
  iframe: $('#js_iframe'),
  url: 'base/jj',
  init: function() {
    const sideBar = new SideBar();
    sideBar.init();
    this.changePage();
  },
  createIframe: function() {
    this.iframe = document.createElement('iframe');
    this.iframe.src = `./js_api/${this.url}.html`;
    this.iframeWrapper.append(this.iframe);
  },
  destroyIframe: function() {
    if (this.iframe === null) {
      return;
    }
    // 把iframe指向空白页面，释放内存
    this.iframe.src = 'about:blank';
    try {
      this.iframe.contentWindow.document.write('');
      this.iframe.contentWindow.document.clear();
    } catch (e) {};
    // 把iframe从页面移除
    this.iframeWrapper.empty();
    this.iframe = null;
  },
  changePage: function() {
    const hash = window.location.hash;
    if (hash) {
      this.url = hash.substring(1);
    }
    this.destroyIframe();
    this.createIframe();
  },
  destroy() {}
};

$(function() {
  apiPage.init();
  if ('onhashchange' in window) {
    window.onhashchange = function(ev) {
      console.log(window.location.hash);
      apiPage.changePage();
    };
  }
});
